package com.guoxin.artconcurrency.chapter5;


import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/**
 * @author guoxin
 * @date 2019/8/4
 */
public class TwinsLockTest {
    private static Lock lock = new TwinsLock();

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            Worker worker = new Worker();
            worker.setDaemon(true);
            worker.start();
        }

        for(int i=0;i<10;i++){
            TimeUnit.SECONDS.sleep(1);
            System.out.println();
        }
    }

    private static class Worker extends Thread {
        @Override
        public void run() {
            while (true) {
                lock.lock();
                try {
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println(Thread.currentThread().getName());
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }
    }
}
